using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class VTouchManager : MonoBehaviour
{
    public List<VTouchInteractNode> nodes;

    [LabelText("开启节点可视化")][OnValueChanged("OdinCall_SwitchVisualNode")] public bool visualNodeEnable = false;

    [ShowIf("visualNodeEnable")] public List<Transform> visualNodePrefabs;

    public void OdinCall_SwitchVisualNode()
    {
        if (visualNodeEnable)
        {
            visualNodePrefabs = new();
            var tempNode = VAnimationDefine.GetPrefab("visualNode").runtimePrefab;
            foreach (var item in nodes)
            {
                var curNode = GameObject.Instantiate(tempNode);
                curNode.transform.localScale = Vector3.one * 0.1f; //缩放到10cm半径
                curNode.transform.parent = item.transform;
                curNode.transform.localPosition = item.touchCollider.center;
                visualNodePrefabs.Add(curNode.transform);
            }
        }
        else
        {
            visualNodePrefabs.Dismiss(x => DestroyImmediate(x.gameObject));
            visualNodePrefabs = null;
        }
    }


    public void ApplyRigUnits()
    {
        var rigComp = gameObject.GetComponent<VRig>();
        if (rigComp == null) return;
        if (rigComp.IKData == null) return;
        nodes = new();
        foreach (var unit in rigComp.IKData)
        {
            if (unit.name == "Head") continue;
            var node = unit.tipTransform.gameObject.AddComponent<VTouchInteractNode>();
            node.OnAdd(this, unit);
            nodes.Add(node);
        }
        print("应用IKData完成节点绑定!");
    }

    public void OnRemove()
    {
        for (int i = nodes.Count - 1; i >= 0; i--)
        {
            var temp = nodes[i];
            nodes[i] = null;
            temp.OnRemove();
        }
    }

    VTouchInteractNode curNode = null;
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) // 0代表左键点击
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 从鼠标位置发射射线

            if (Physics.Raycast(ray, out hit))
            {
                Collider hitCollider = hit.collider;
                if (hitCollider != null)
                {
                    var node = hitCollider.GetComponent<VTouchInteractNode>();
                    node.OnMouseRayEnter();
                    curNode = node;
                }
            }
        }

        if (Input.GetMouseButton(0))
        {
            if (curNode != null)
            {
                var nodeTransform = curNode.transform;

                // 获取鼠标增量（相对上一帧）
                float deltaX = Input.GetAxis("Mouse X");
                float deltaY = Input.GetAxis("Mouse Y");

                // 定义移动速度（可调节）
                float moveSpeed = 1; // 调小值，防止跳太快

                // 根据增量计算平移方向（这里以水平X、垂直Y平面为例）
                Vector3 movement = new Vector3(deltaX, deltaY, 0) * moveSpeed;

                // 应用平移
                nodeTransform.Translate(movement, Space.World);
                print("moving");

            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            curNode = null;
        }


    }



}
